package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    //客户列表分页
    @Override
    public Map<String, Object> page(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> list=customerDao.page(offSet,rows,customerName);
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }
    //客户的添加或修改
    @Override
    public ServiceVO save(Integer customerId, Customer customer) {
        int count=0;
        if(customerId!=null){
           count= customerDao.update(customer);
        }else {
            count=customerDao.save(customer);
        }
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO<>(250,"周傲");
    }

    //删除客户
    @Transactional
    @Override
    public ServiceVO delete(String ids) {
        int count=0;
        String[] split = ids.split(",");
        for (String s : split) {
           count= customerDao.delete(s);
        }
        if(count>0){
            return new ServiceVO<>(100,"请求成功");
        }
        return new ServiceVO<>(250,"周傲");
    }

}
